'use strict';

angular.module('ProduceWork').controller('WhqohTransClientController', ['$scope', '$resource', 'BaseModule', 'BaseEnum', 'ProduceWorkEnum', '$location', 'Authentication','BaseFunction',
    function($scope, $resource, BaseModule, BaseEnum, ProduceWorkEnum, $location, Authentication, BaseFunction) {
        $scope.CreateErpDocTypeEnum = BaseFunction.getEnum('ProduceWorkEnum.CreateErpDocType');
        $scope.All = false;
        $scope.IfCreatedERPDoc = false;
        $scope.NotCreatedERPDoc = true;

        BaseModule.initModule($scope, 'RcvRecord');
        $scope.Module.RcvRecord.LoadMode = BaseEnum.LoadMode.CacheAndPaging;
        $scope.Module.RcvRecord.Condition = { IsCreatedERPDoc:0 };
        $scope.Module.RcvRecord.PageSize = 15;
        $scope.Module.RcvRecord.Populate = [{
            path: 'RcvUser',
            select: 'Name Department Code',
            model: 'User',
            populate: {
                path: 'Department',
                select: 'Name',
                model: 'Department'
            }
        }];

        BaseModule.initModule($scope, 'RcvRecordDetial');
        $scope.Module.RcvRecordDetial.LoadMode = BaseEnum.LoadMode.CacheAndPaging;
        $scope.Module.RcvRecordDetial.PageSize = 15;

        //全部记录
        $scope.LoadAll = function() {
            if ($scope.All) {
                $scope.IfCreatedERPDoc = false;
                $scope.NotCreatedERPDoc = false;
                LoadData();
            }
            else{
            }
        }

        //获取已经创建单据的记录
        $scope.LoadCreatedERPDoc = function() {
            if ($scope.IfCreatedERPDoc) {
                $scope.All = false;
                $scope.NotCreatedERPDoc = false;
                $scope.$digest();

                LoadData();
            }
            else{
            }
        }

        //获取未创建单据的记录
        $scope.LoadNotCreateERPDoc = function() {
            if ($scope.NotCreatedERPDoc) {
                $scope.All = false;
                $scope.IfCreatedERPDoc = false;
                $scope.$digest();

                LoadData();
            }
            else{
            }
        }

        function LoadData(){
            var condit = {};
            if ($scope.IfCreatedERPDoc == true) {
                condit['IsCreatedERPDoc'] = 1
            } else if ($scope.NotCreatedERPDoc == true) {
                condit['IsCreatedERPDoc'] = 0
            };

            $scope.Module.RcvRecord.Condition=condit;
            $scope.Module.RcvRecord.load();
        };

        $scope.CreateDoc = function(){
            var selectedRecords = $scope.Module.RcvRecord.SelectedRecords;

            $resource('/api/barcodeinfo/CreateErpDoc').save(
                selectedRecords, 
                function (res) {
                    res = res.toJSON();
                    if (res.Ifsuccess){
                        BaseFunction.alert("同步成功", "生单成功");
                    }
                    else{
                        BaseFunction.alert("同步失败", res.data);
                    }
                }
            );
        }

        $scope.rowChange = function(){
            var fouscRecord = $scope.Module.RcvRecord.FocusedRecord;
            if (fouscRecord){
                $scope.Module.RcvRecordDetial.Condition = {RcvRecord: fouscRecord._id};
                $scope.Module.RcvRecordDetial.load();
            }
        };
    }
])